#include "connectmysqldialog.h"
#include "ui_connectmysqldialog.h"

ConnectMySQLDialog::ConnectMySQLDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConnectMySQLDialog)
{
    ui->setupUi(this);
    iniDialog();

    connect(this,&ConnectMySQLDialog::sigUpdateDBTreeWidget,
            AppEvent::getInstance(),&AppEvent::on_UpdateDBTreeWidget);
}

ConnectMySQLDialog::~ConnectMySQLDialog()
{
    delete ui;
}

void ConnectMySQLDialog::iniDialog()
{
    QLabel *labelHost=new QLabel(QObject::tr("Host:"));
    QLabel *labelPort=new QLabel(QObject::tr("Port:"));
    QLabel *labelUser=new QLabel(QObject::tr("User:"));
    QLabel *labelPassword=new QLabel(QObject::tr("Password:"));
    QLabel *labelDatabase=new QLabel(QObject::tr("Database;"));

    editHost=new QLineEdit("localhost");
    editPort=new QLineEdit("3306");
    editUser=new QLineEdit("root");
    editPassword=new QLineEdit();
    editPassword->setEchoMode(QLineEdit::Password);
    editDatabase=new QLineEdit();

    QPushButton *btnOK=new QPushButton(QObject::tr("OK"));
    QPushButton *btnCancel=new QPushButton(QObject::tr("Cancel"));

    QVBoxLayout *layoutMainDialog=new QVBoxLayout();

    QHBoxLayout *layoutHost=new QHBoxLayout();
    layoutHost->addWidget(labelHost);
    layoutHost->addWidget(editHost);
    QHBoxLayout *layoutPort=new QHBoxLayout();
    layoutPort->addWidget(labelPort);
    layoutPort->addWidget(editPort);
    QHBoxLayout *layoutUser=new QHBoxLayout();
    layoutUser->addWidget(labelUser);
    layoutUser->addWidget(editUser);
    QHBoxLayout *layoutPassword=new QHBoxLayout();
    layoutPassword->addWidget(labelPassword);
    layoutPassword->addWidget(editPassword);
    QHBoxLayout *layoutDatabase=new QHBoxLayout();
    layoutDatabase->addWidget(labelDatabase);
    layoutDatabase->addWidget(editDatabase);
    QHBoxLayout *layoutButton=new QHBoxLayout();
    layoutButton->addWidget(btnOK);
    layoutButton->addWidget(btnCancel);

    layoutMainDialog->addLayout(layoutHost);
    layoutMainDialog->addLayout(layoutPort);
    layoutMainDialog->addLayout(layoutUser);
    layoutMainDialog->addLayout(layoutPassword);
    layoutMainDialog->addLayout(layoutDatabase);
    layoutMainDialog->addSpacing(10);
    layoutMainDialog->addLayout(layoutButton);

    this->setLayout(layoutMainDialog);

    connect(btnOK,&QPushButton::clicked,this,&ConnectMySQLDialog::on_btnOK_clicked);
    connect(btnCancel,&QPushButton::clicked,this,&ConnectMySQLDialog::on_btnCancel_clicked);

}
bool ConnectMySQLDialog::testConnection(QString host,int port,QString user,QString password,QString database)
{
    /*
    DBMySQL=QSqlDatabase::addDatabase("QMYSQL");
    DBMySQL.setHostName(host);
    DBMySQL.setPort(port);
    DBMySQL.setUserName(user);
    DBMySQL.setPassword(password);
    DBMySQL.setDatabaseName(database);

    if(DBMySQL.open())
    {
        QMessageBox::information(this,"正确","数据库连接成功\n",QMessageBox::Ok,QMessageBox::NoButton);
        //DBMySQL.close();
        return true;
    }
    else
    {
        QMessageBox::information(this,"失败","数据库连接失败\n",QMessageBox::Ok,QMessageBox::NoButton);
        return false;
    }
    */
    return true;
}

QList<QStandardItem*> ConnectMySQLDialog::readDatabase()
{
    QList<QStandardItem*> listDBContext;

    QStandardItem *itemDBName=new QStandardItem(mysqlDatabase);
    QStandardItem *itemDBNameType=new QStandardItem(QObject::tr("databaseName"));
    listDBContext<<itemDBName<<itemDBNameType;

    QStandardItem *itemFLayerFolder=new QStandardItem(QObject::tr("feature layers"));
    QStandardItem *itemFLayerFolderType=new QStandardItem(QObject::tr("feature folder"));
    QList<QStandardItem*> listFLayerFolder;
    listFLayerFolder<<itemFLayerFolder<<itemFLayerFolderType;
    itemDBName->appendRow(listFLayerFolder);

    QStandardItem *itemSchemeFolder=new QStandardItem(QObject::tr("scheme folder"));
    QStandardItem *itemSchemeFolderType=new QStandardItem(QObject::tr("scheme folder"));
    QList<QStandardItem*> listSchemeFolder;
    listSchemeFolder<<itemSchemeFolder<<itemSchemeFolderType;
    itemDBName->appendRow(listSchemeFolder);

    return listDBContext;
}

QTreeWidgetItem* ConnectMySQLDialog::readDBContext()
{
    QTreeWidgetItem* dbName=new QTreeWidgetItem(DBTreeNodeType::databaseName);
    dbName->setText(DBTreeColNum::colItem,mysqlDatabase);
    QTreeWidgetItem* featureGroup=new QTreeWidgetItem(DBTreeNodeType::featureGroup);
    featureGroup->setText(DBTreeColNum::colItem,QObject::tr("Feature Group"));
    QTreeWidgetItem* schemeGroup=new QTreeWidgetItem(DBTreeNodeType::schemeGroup);
    schemeGroup->setText(DBTreeColNum::colItem,QObject::tr("Scheme Group"));

    dbName->addChild(featureGroup);
    dbName->addChild(schemeGroup);

    return dbName;

}

void ConnectMySQLDialog::on_btnOK_clicked()
{
    mysqlHost=editHost->text();
    mysqlPort=editPort->text().toInt();
    mysqlUser=editUser->text();
    mysqlPassword=editPassword->text();
    mysqlDatabase=editDatabase->text();

    bool success=testConnection(mysqlHost,mysqlPort,mysqlUser,mysqlPassword,mysqlDatabase);
    if(success)
    {
        //QList<QStandardItem*> context=readDatabase();
        //emit sigUpdateDBTreeView("mysqlroot",context);
        QTreeWidgetItem *context=readDBContext();
        emit sigUpdateDBTreeWidget("mysqlroot",context);

    }
    this->close();
}

void ConnectMySQLDialog::on_btnCancel_clicked()
{
    this->close();

}
